package org.auf.model.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import org.apache.log4j.Logger;
import org.auf.model.dao.IUserDAO;
import org.auf.to.User;

public class UserDAOJDBC implements IUserDAO {
	static Logger log = Logger.getLogger(UserDAOJDBC.class.getName());

    private static UserDAOJDBC INSTANCE = null;
    
    private UserDAOJDBC() {}
    
    private synchronized static void createInstance() {
        if (INSTANCE == null) { 
            INSTANCE = new UserDAOJDBC();
        }
    }
 
    public static IUserDAO getInstance() {
        if (INSTANCE == null) createInstance();
        return INSTANCE;
    }


	
	@Override
	public boolean insertUser(User user) {
		int updates=0;
		ConnectionManager cm = ConnectionManager.getInstance();
		Connection db = cm.checkOut();
		
		try{
			PreparedStatement st = db.prepareStatement("INSERT INTO tbluser (id,name,password,mail,geeks,score) values (?,?,?,?,?,?)");
			st.setString(1, user.getId());
			st.setString(2, user.getName());
			st.setString(3, user.getPassword());
			st.setString(4, user.getMail());
			st.setBoolean(5, user.getGeek());
			st.setInt (6 ,user.getScore());
			updates =st.executeUpdate();
		
		}catch(Exception e){updates=-1;log.error("Error al generar la sentencia SQL y ejecutarla: "+e);}

		cm.checkIn(db);
		// TODO Auto-generated method stub
		return updates==1;
	}

	@Override
	public User selectUserByName(String name) {
		User user = new User();
		
		ConnectionManager cm = ConnectionManager.getInstance();
		Connection db = cm.checkOut();
		
		try{
			PreparedStatement st = db.prepareStatement("SELECT * FROM tbluser WHERE name=?");
			st.setString(1, name);
			ResultSet rs = st.executeQuery();
		
			if (rs.next()){
				user.setId( rs.getString("id"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setMail(rs.getString("mail"));
				user.setGeek(rs.getBoolean("geeks"));
				user.setScore(rs.getInt("score"));
				if (rs.next())user=null;
			}else user=null;
		
		}catch(Exception e){user=null;log.error("Error al generar la sentencia SQL y ejecutarla: "+e);}

		cm.checkIn(db);
		// TODO Auto-generated method stub
		return user;
	}

	@Override
	public User selectUserByid(String id) {
		User user = new User();
		
		ConnectionManager cm = ConnectionManager.getInstance();
		Connection db = cm.checkOut();
		
		try{
			PreparedStatement st = db.prepareStatement("SELECT * FROM tbluser WHERE id=?");
			st.setString(1, id);
			ResultSet rs = st.executeQuery();
		
			if (rs.next()){
				
				user.setId( rs.getString("id"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				user.setMail(rs.getString("mail"));
				user.setGeek(rs.getBoolean("geeks"));
				user.setScore(rs.getInt("score"));
				if (rs.next())user=null;
			}else user=null;
		
		}catch(Exception e){user=null;log.error("Error al generar la sentencia SQL y ejecutarla: "+e);}

		cm.checkIn(db);
		// TODO Auto-generated method stub
		return user;
	}

	@Override
	public boolean updateUserScore(User user) {
		int updates=0;
		ConnectionManager cm = ConnectionManager.getInstance();
		Connection db = cm.checkOut();
		
		try{
			PreparedStatement st = db.prepareStatement("UPDATE tbluser SET score = ? WHERE id = ?");
			st.setInt(1, user.getScore());
			st.setString(2, user.getId());
			updates =st.executeUpdate();
		
		}catch(Exception e){updates=-1;log.error("Error al generar la sentencia SQL y ejecutarla: "+e);}

		cm.checkIn(db);
		// TODO Auto-generated method stub
		return updates==1;
	}

}
